package com.amkai.aes.aesws;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.amkai.aes.aescommon.service.binary.job.Job;
import com.amkai.aes.aescommon.service.result.DefaultResult;
import com.amkai.aes.webservice.binary.JobManager;

/**
 * Servlet implementation class AesServlet
 */
@Deprecated
public class AesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
    	super.init(config);
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getOutputStream().println("<html><body><h1>Amkai Enterprise Servlet</h1></body></html>");
		response.getOutputStream().flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			ObjectInputStream input = new ObjectInputStream(new GZIPInputStream(new BufferedInputStream(request.getInputStream())));
			Job job = (Job) input.readObject();
			input.close();
			Serializable result = JobManager.processJobObject(job, job.getInputObject());
			GZIPOutputStream gout = new GZIPOutputStream(new BufferedOutputStream(response.getOutputStream()));
			ObjectOutputStream output = new ObjectOutputStream(gout);
			DefaultResult dr = new DefaultResult();
			dr.setDefaultResult(result);
			//System.out.println(dr.defaultResult);
	        output.writeObject(dr);
	        //gout.flush();
	        output.flush();
	        output.close();
	        response.flushBuffer();
	        //System.out.println("Flush result + buffer: " + dr);
		} catch (Throwable t) {
			t.printStackTrace();
			throw new ServletException(t);
		}
	}

}
